﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Dynamic.Core;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using PMS.SANY.Entity;
using PMS.SANY.IBusiness;
using PMS.SANY.Util;
using PMS.SANY.Models;

namespace PMS.SANY.Business
{
    public class MaterielSortingSequenceBusiness : BusRepository<MaterielSortingSequence>, IMaterielSortingSequenceBusiness, IScopedDependency
    {
        private IServiceProvider SvcProvider { get; set; }
        public MaterielSortingSequenceBusiness(GDbContext context, IServiceProvider svcProvider)
            : base(context)
        {
            this.SvcProvider = svcProvider;
        }

        public async Task<PageResult<MaterielSortingSequence>> GetPageResultAsync(PageInput<MaterielSortingSequenceQM> query)
        {
            var queryable = this.GetQueryable(true);
            queryable = queryable.Include(i => i.SortingConfig)
                .Include(i => i.SortingLocation).ThenInclude(ti => ti.Location)
                .Include(i => i.SortingLocation).ThenInclude(ti => ti.Spec);

            var search = query.Search;
            queryable = queryable.Where(w => w.SortingConfigId == search.SortingConfigId);


            var result = await this.GetPageResultAsync(queryable, query);
            if (!search.Id.IsNullOrEmpty())
            {
                if (!result.Data.Any(w => w.Id == search.Id))
                {
                    var idOne = await this.GetQueryable(true).SingleOrDefaultAsync(w => w.Id == search.Id);
                    result.Data.Add(idOne);
                    result.Total += 1;
                }
            }
            return result;
        }

        public async Task<int> RemoveAsync(List<string> ids)
        {
            var deleteList = await this.GetListAsync(ids);
            var sortingConfigId = deleteList[0].SortingConfigId;
            var result = await this.DeleteAsync(deleteList);
            var modifyList = await this.GetListAsync(i => i.SortingConfigId == sortingConfigId);

            if (modifyList != null && modifyList.Count > 0)
            {
                var list = (from u in modifyList orderby u.OrderNo ascending select u).ToList();
                for (var idx = 0; idx < list.Count; idx++)
                {
                    list[idx].OrderNo = idx + 1;
                    await this.AddOrUpdateAsync(list[idx]);
                }
            }

            return 1;
        }

        public async Task<int> SortingAsync(string id, bool isUp)
        {
            var entity = await this.GetAsync(id);
            var sortingConfigId = entity.SortingConfigId;
            if(isUp)
            {
                var preNo = entity.OrderNo - 1;
                var pre = await this.GetAsync(i => i.OrderNo == preNo && i.SortingConfigId == sortingConfigId);
                if (pre != null)
                {
                    pre.OrderNo += 1;
                    entity.OrderNo -= 1;

                    await this.UpdateAsync(entity);
                    await this.UpdateAsync(pre);

                    return 1;
                }
            }
            else
            {
                var nextNo = entity.OrderNo + 1;
                var next = await this.GetAsync(i => i.OrderNo == nextNo && i.SortingConfigId == sortingConfigId);
                if (next != null)
                {
                    next.OrderNo -= 1;
                    entity.OrderNo += 1;

                    await this.UpdateAsync(entity);
                    await this.UpdateAsync(next);

                    return 1;
                }
            }

            return 0;
        }
    }
}
